/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved
*
*  This file is part of the GroupMe! Project. You can utilize
*  software of the GroupMe! project under the terms of the 
*  GNU Lesser General Public License as published by the
*  Free Software Foundation in version 2.1.
* 
*  The GNU Lesser General Public License can be found at:
*  http://www.gnu.org/licenses/lgpl.html
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.export;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import de.nava.informa.core.ChannelExporterIF;
import de.nava.informa.core.ChannelIF;
import de.nava.informa.exporters.RSS_1_0_Exporter;
import de.nava.informa.exporters.RSS_2_0_Exporter;
import de.nava.informa.impl.basic.ChannelBuilder;
import java.io.IOException;

import java.io.StringWriter;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import net.groupme.exceptions.ExportFailedException;

import net.groupme.export.constants.General;
import net.groupme.export.rss.GroupExport;
import net.groupme.model.Group;
import net.groupme.model.ObjectFactoryProperties;
import net.groupme.model.Resource;
import net.groupme.model.Tag;

import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * This class provides...
 *
 * @author Daniel Plappert
 * @version 0.1
 */
public class RssExport extends AbstractExporter {
    

    public RssExport() {};
    
    
    /**
     *
     *
     * @return String RDF-Data
     */
    public String doExport() throws ExportFailedException {
        
        if ( groupData == null ) {
            throw new ExportFailedException("Error: no data was set. RSS export is only " +
                "available for groups and tag search. Please submit a groupID or select \"export-groups\" as export mode");
        }
        
        StringWriter writer = new StringWriter();
        
        ChannelBuilder builder = new ChannelBuilder();
        ChannelExporterIF exporter = new RSS_2_0_Exporter(writer, "UTF-8");
        ChannelIF channel = builder.createChannel("search-result");
        GroupExport export = new GroupExport(builder);
        
        if ( groupIsExportedById() ) {
            channel = builder.createChannel(groupData.iterator().next().getName());
            export.setExportGroupAsChannel(true);
        } else {
            String usedTags = "[";
            for(Tag tag : keywords) {
                usedTags += tag.getKeyword() + ", ";
            }
            usedTags = (usedTags.length() > 2 ? usedTags.substring(0, usedTags.length() - 2) + "]" : "[]");
            channel.setDescription("search result when searching for resources or groups tagged with " + usedTags);
            export.setExportGroupAsChannel(false);
        }

        export.setData(groupData);
        export.exportData(channel);        
        
        channel.setLanguage("en");
        channel.setGenerator("groupme! rss export");
        channel.setPubDate(new Date());
        
        try {
            exporter.write(channel);
        } catch ( IOException ex ) {
            throw new ExportFailedException("RSS Export Failed. Message was: " + ex.toString());
        }
            
        return writer.toString();
    }
    

}
